package com.yfker.pda;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yfker.pda.model.InfusionBottleModel;

import java.util.ArrayList;
import java.util.List;

public class InfusionBottleAdapter extends RecyclerView.Adapter<InfusionBottleAdapter.ViewHolder> {

    private List<InfusionBottleModel> infusionBottleModels;

    @SuppressLint("NotifyDataSetChanged")
    public void addBottle(InfusionBottleModel model) {
        if (infusionBottleModels == null) {
            infusionBottleModels = new ArrayList<>();
        }
        infusionBottleModels.add(model);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_infusion_bottle_layout, parent, false);
        return new ViewHolder(root);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        InfusionBottleModel infusionBottleModel = infusionBottleModels.get(position);
        holder.mTvBottleIndex.setText("瓶号: " + "第" + infusionBottleModel.getBottleNumber() + "瓶");
        holder.mTvDrugCapacity.setText("容量: " + infusionBottleModel.getCapacity() + "ml");
        holder.mTvDrugName.setText("名称: " + infusionBottleModel.getDrugName());
    }

    @Override
    public int getItemCount() {
        return infusionBottleModels == null ? 0 : infusionBottleModels.size();
    }

    public void clear() {
        if (infusionBottleModels != null)
            infusionBottleModels.clear();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mTvBottleIndex;

        public TextView mTvDrugName;

        public TextView mTvDrugCapacity;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mTvBottleIndex = itemView.findViewById(R.id.tv_bottle_index);
            mTvDrugName = itemView.findViewById(R.id.tv_durg_name);
            mTvDrugCapacity = itemView.findViewById(R.id.tv_drug_capacity);
        }
    }
}
